package xt.outfood.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import xt.outfood.common.JacksonObjectMapper;

import java.util.List;

/**
 * 使用WebMvcConfigurationSupport做拦截器
 *
 * @author 小小爱欧
 * @EnableSwagger2
 * @EnableKnife4j 开启文档接口
 * 静态资源路径映射，消息转换，knife4j文档生成
 * @date 2022/7/19 14:58
 * 编写配置类，访问静态资源
 */
@Slf4j
@Configuration
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
    /**
     * 设置静态资源映射路径
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("静态资源映射.............");
        //设置swagger资源路径映射
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        //设置静态资源映射
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

    //  扩展SpringMvc的消息转换器
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("自定义消息转化器 被调用!");
        // 创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 设置对象转换器，底层使用JackSON 将Java对象 转化为JSON
        messageConverter.setObjectMapper(new JacksonObjectMapper());

        // 将上面的消息转换器对象追加到SpringMVC的 转换器容器 的第一个位置(优先采用下标为 0 位置的消息转换器)
        converters.add(0, messageConverter);


    }

//    @Autowired
//    private IpCountService ipCountService;

    /**
     * 注册拦截器接口，测试对所有请求都进行拦截,获取请求的IP统计
     * //     * @param registry
     */

//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        log.info("拦截器生效中.................");
//        registry.addInterceptor(new IpCountInterceptor()).addPathPatterns("/employee/**");
//    }

//    @Bean
//    public IpCountInterceptor ipCountInterceptor(){
//        return new IpCountInterceptor();
//    }

    // 在配置类中声明一个Docket类型的bean, 通过该bean来指定生成文档的信息
    @Bean
    public Docket createRestApi() {
        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //更改为自己项目的路径:
                .apis(RequestHandlerSelectors.basePackage("xt.outfood.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("汉武大地传奇")
                .version("1.0")
                .description("九月是个谎言")
                .build();
    }


}
